package com.example;

import com.alibaba.fastjson.JSONObject;
import com.example.model.Country;
import org.hibernate.Session;
import org.hibernate.annotations.common.reflection.ReflectionUtil;
import org.hibernate.procedure.ProcedureCall;

import javax.persistence.*;
import java.util.List;

public class ExampleMain {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit");
        EntityManager em = emf.createEntityManager();
        StoredProcedureQuery query = em.createStoredProcedureQuery("sp_findBetween",Country.class);
        query.registerStoredProcedureParameter("min",Integer.class,ParameterMode.IN);
        query.registerStoredProcedureParameter("max",Integer.class,ParameterMode.IN);
        query.setParameter("min",200000);
        query.setParameter("max",1000000);
        boolean b = query.execute();
        if (b) {
            List<Country> countries = query.getResultList();
            for (Country country : countries) {
                System.err.println(JSONObject.toJSONString(country,false));
            }
        }

        em.close();
        emf.close();
    }

    private static void method3(EntityManager em) {
        Query query = em.createQuery("select c from Country c where c.continent = 'Asia' and c.population between 200000 and 1000000 order by c.capital asc");
        List<Country> countries = query.getResultList();
        for (Country country : countries) {
            System.err.println(JSONObject.toJSONString(country,false));
        }
    }

    private static void method2(EntityManagerFactory emf) {
        Session session = getSession(emf);
        ProcedureCall procedureCall = session.createStoredProcedureCall("citycount");
        /*procedureCall.registerStoredProcedureParameter("contry",String.class,ParameterMode.IN);
        procedureCall.registerStoredProcedureParameter("citycount",Integer.class,ParameterMode.OUT);*/
        procedureCall.registerParameter("contry",String.class,ParameterMode.IN).bindValue("NLD");
        procedureCall.registerParameter("citycount",Integer.class,ParameterMode.OUT);
        Integer count = (Integer) procedureCall.getOutputs().getOutputParameterValue("citycount");
        System.err.println("count=" + count);
        session.close();
    }

    private static void method1(EntityManager em) {
        StoredProcedureQuery query = em.createStoredProcedureQuery("citycount");
        query.registerStoredProcedureParameter("contry",String.class, ParameterMode.IN);
        query.registerStoredProcedureParameter("citycount",Integer.class, ParameterMode.OUT);
        query.setParameter("contry","NLD");
        query.execute();
        Integer count = (Integer) query.getOutputParameterValue("citycount");
        System.err.println("count=" + count);
    }

    public static Session getSession(EntityManagerFactory emf) {
        EntityManager entityManager = emf.createEntityManager();
        Session session = entityManager.unwrap(org.hibernate.Session.class);
        return session;
    }

}
